package com.midele.goodstuff.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.midele.goodstuff.ui.search.SearchPageIntent
import com.midele.goodstuff.ui.search.SearchPageState
import com.midele.goodstuff.util.LogUtils
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class SearchViewModel @Inject constructor() : ViewModel() {

    private val _searchPageState = MutableStateFlow(SearchPageState())
    val searchPageState: StateFlow<SearchPageState> = _searchPageState.asStateFlow()

    fun processIntent(intent: SearchPageIntent) {

        viewModelScope.launch {
             when (intent) {
                 is SearchPageIntent.SearchGoodsData -> {
                     LogUtils.d("SearchScreen", "SearchScreen searchData>>> ${intent.searchText} 2222222>>>>>>>>>>>>>>>>>")
                     _searchPageState.value.searchText = intent.searchText
                     _searchPageState.value = _searchPageState.value
                 }
                 is SearchPageIntent.GetSearchHistoryData -> {getSearchHistoryData()}
                 is SearchPageIntent.GetRecommendData -> {getRecommendData()}
                 is SearchPageIntent.DeleteSearchHistory -> {""}
                 else -> ""
             }
        }
    }

     fun getRecommendData() {
       _searchPageState.value.recommendList = listOf("天珠1","天珠2","天珠3")
      // _searchPageState.value = _searchPageState.value
    }

    fun getSearchHistoryData() {
        _searchPageState.value.searchHistoryList = listOf("111","222","333")
        //_searchPageState.value = _searchPageState.value

    }


}